package DaoImpl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import Dao.StlistDao;
import Entity.Stclass;
import Entity.Stlist;
import Util.HibernateUtil;

public class StlistDaoImpl implements StlistDao{

	
   //删除社团
	@Override
	public boolean deleteSt(Stlist st) {
		Transaction ts = null;
		try{
			Session session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			session.delete(st);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
            return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
		}
	}
	
	//更新社团
	@Override
	public boolean updateSt(Stlist st) {
		Transaction ts=null;
		try {
			Session session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			session.update(st);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
		}
	}
	//按Id查找社团
	@Override
	public Stlist findSt(String id) {
		// TODO Auto-generated method stub
		Transaction ts=null;
		String hql="";
		try {
			Session session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			hql="from Stlist where stId=?";
			Query query=session.createQuery(hql);
			query.setParameter(0, id);
			List<Stlist> list=query.list();
			Stlist st=list.get(0);
			ts.commit();
			return st;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return null;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
		}
	}
	//查找全部社团
	@Override
	public List<Stlist> findAllSt() {
		// TODO Auto-generated method stub
		Transaction ts=null;
		String hql="";
		try {
			Session session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			hql="from Stlist";
			Query query=session.createQuery(hql);
			List<Stlist> list=query.list();
			ts.commit();
			return list;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return null;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
		}
	
	}
	//添加社团
	@Override
	public boolean addSt(Stlist st) {
		// TODO Auto-generated method stub
		Transaction ts = null;
		try{
			Session session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			session.save(st);
			ts.commit();
			
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
            return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
		}
		return false;
	}

	//把ID为stid 的社团添加进ID为stcid的类别中
	@Override
	public boolean SttoStclass(String stid, int stcid) {
		Transaction ts = null;
		try{
			Session session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			Stlist st=(Stlist) session.get(Stlist.class, stid);
			Stclass stc=(Stclass) session.get(Stclass.class, stcid);
			stc.getSttypelist().add(st);
			st.setStc(stc);
			session.save(stc);
			session.save(st);
			ts.commit();
			
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
            return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
		}
		return false;
	}

}
